package com.informa.unitils.scriptsources;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
	
public class MySqlToHyperSonicAdaptingClasspathScriptSource extends ClasspathScriptSource {
	
	protected String preprocess(String original) {
		String intSizesRemoved = Pattern.compile("int\\(\\d*\\)", Pattern.CASE_INSENSITIVE).matcher(original).replaceAll("int");
		String unsignedRemoved = Pattern.compile("unsigned", Pattern.CASE_INSENSITIVE).matcher(intSizesRemoved).replaceAll("");
		String autoIncrementReplacedWithIdentity = Pattern.compile("auto_increment", Pattern.CASE_INSENSITIVE).matcher(unsignedRemoved).replaceAll("IDENTITY");
		String longBlobReplacedWithObject = Pattern.compile("longblob", Pattern.CASE_INSENSITIVE).matcher(autoIncrementReplacedWithIdentity).replaceAll("LONGVARBINARY");
		String bitSizesRemoved = Pattern.compile("bit\\(\\d*\\)", Pattern.CASE_INSENSITIVE).matcher(longBlobReplacedWithObject).replaceAll("bit");
		String modifyLineChangedToAlterColumn = changeModifyLineToAlterColumn(bitSizesRemoved);		
		String engineRemoved = removeEngine(modifyLineChangedToAlterColumn);

		String reorderDefaultValues = engineRemoved.toLowerCase().replace("not null default '0'", "default 0 NOT NULL");
		
		String foreignKeyChecksRemoved = reorderDefaultValues.replace("set foreign_key_checks = 0;", "").replace("set foreign_key_checks = 1;", "");;
		
		String reversedWordQuotesRemoved = foreignKeyChecksRemoved.replaceAll("`day`", "day");
		
		String replacedChangeColumnNameToRenameColumnName = replaceChangeColumnNameToRename(reversedWordQuotesRemoved);
		
		return "SET IGNORECASE true; "  + replacedChangeColumnNameToRenameColumnName;
	}

	private String removeEngine(String bitSizesRemoved) {
		String removedEngine = Pattern.compile("ENGINE=innodb DEFAULT CHARSET=utf8", Pattern.CASE_INSENSITIVE).matcher(bitSizesRemoved).replaceAll("");

		String removedEngineType2 = Pattern.compile("ROW_FORMAT=FIXED", Pattern.CASE_INSENSITIVE).matcher(removedEngine).replaceAll("");
		return removedEngineType2;
	}
	
	private String replaceChangeColumnNameToRename(String text) {
		Pattern pattern = Pattern.compile("^alter table.*change.*;",
				Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);

		Matcher matcher = pattern.matcher(text);

		while (true) {
			matcher.find();

			if(matcher.hitEnd()) {
				break;
			}
			
			String modifyLine = matcher.group();

			String[] modifyLineElements = modifyLine.split(" ");

			String replacementString = modifyLineElements[0]+ " " + modifyLineElements[1]+ " " + modifyLineElements[2]	+ " alter column " + modifyLineElements[4] + " rename to " + modifyLineElements[5] + ";"; 
			
			text = text.replace(modifyLine, replacementString);
		}
		
		return text;
	}

	private String changeModifyLineToAlterColumn(String text) {
		Pattern pattern = Pattern.compile(".*MODIFY.*NULL.*$",
				Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

		Matcher matcher = pattern.matcher(text);

		while (true) {
			matcher.find();

			if(matcher.hitEnd()) {
				break;
			}
			
			String modifyLine = matcher.group();

			String[] modifyLineElements = modifyLine.split(" ");

			String replacementString = "ALTER COLUMN " + modifyLineElements[1]	+ " SET NOT NULL";
			
			text = text.replace(modifyLine, replacementString);
		}
		return text;
	}
}
